//
//  contentViewController.m
//  ForengnMenu
//
//  Created by wenzhaoxu on 16/1/21.
//  Copyright © 2016年 余旺. All rights reserved.
//

#import "contentViewController.h"
#import "contentTableViewCell.h"
#import "MenuContentViewController.h"

@interface contentViewController ()<UITableViewDataSource,UITableViewDelegate,contentTableViewCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property BOOL isOpen;

@property int praiseNumber;

@end

@implementation contentViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"Icon_return"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 30, 30);
    button.layer.cornerRadius = button.frame.size.height/2.0f;
    button.layer.masksToBounds = YES;
    
    [button addTarget:self action:@selector(retun) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.myTableView.backgroundColor =[UIColor colorWithRed:246/255.0f green:247/255.0f blue:239/255.0f alpha:1.0f];
    
    self.myTableView.separatorStyle = UITableViewCellStyleDefault;
    
    UIButton * centerBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    [centerBnt setImage:[UIImage imageNamed:@"Icon_Search_Box"] forState:UIControlStateNormal];
    
    centerBnt.frame = CGRectMake(0, 0, 205, 30);
    
    [centerBnt addTarget:self action:@selector(serarchBoxAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 215, 30)];
    
    [view addSubview:centerBnt];
    
    self.navigationItem.titleView = view;
    
    UIButton * rightBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBnt setTitle:@"搜索" forState:UIControlStateNormal];
    [rightBnt setTitleColor:[UIColor colorWithRed:248/255.0f green:102/255.0f blue:79/255.0f alpha:1.0f] forState:UIControlStateNormal];
    
    rightBnt.titleLabel.font = [UIFont systemFontOfSize:16];
    
    rightBnt.frame = CGRectMake(0, 0, 40, 30);
    
    [rightBnt addTarget:self action:@selector(searchBar) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBnt];
}
-(void)retun{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)searchBar{

}
-(void)serarchBoxAction{
    
   [self performSegueWithIdentifier:@"sousuo" sender:nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.recipeArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * identifier = @"Cell";
    
    contentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    NSDictionary * array = self.recipeArray[indexPath.section];
    
    NSDictionary * dicArr = array[@"Content"];
    
    cell.foodImageView.image = [UIImage imageNamed:dicArr[@"CookingImage"]];
    if (!cell.foodImageView.image) {
        cell.foodImageView.image = dicArr[@"CookingImageData"];
    }

    
    cell.myTextLabel.text = dicArr[@"RecipeShow"];
    
    cell.diffcultLabel.text = dicArr[@"CookingDifficulty"];
    
    cell.timeLabel.text = dicArr[@"CookingTime"];
    
    cell.praiseLabel.text = dicArr[@"Praise"];
    
    cell.titleLbael.text = dicArr[@"RecipeName"];
    
    cell.delegate = self;
    
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 14;
    }else{
        return 12;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSDictionary * array = self.recipeArray[indexPath.section];
    
    [self performSegueWithIdentifier:@"details" sender:array];
    
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString:@"details"]) {
        
        MenuContentViewController * content = segue.destinationViewController;
        
        content.recipeArray = sender;

    }

}
//收藏
-(void)collectionAction:(contentTableViewCell*)cell{
    
    NSIndexPath *indexPath = [_myTableView indexPathForCell:cell];
    
    NSMutableDictionary * dic = [self.recipeArray[indexPath.section] mutableCopy];
    
    NSMutableDictionary * dicArr = [dic[@"Content"] mutableCopy];
    
    int a = [[NSString stringWithFormat:@"%@",dicArr[@"Praise"]] intValue];
    
    if (_isOpen) {
        [cell.collectionBnt setImage:[UIImage imageNamed:@"Icon_collection"] forState:UIControlStateNormal];
        NSString * praiseCount = [NSString stringWithFormat:@"%d",a-1];
        
        cell.praiseLabel.text = praiseCount;
        
        dicArr[@"Praise"] = praiseCount;
        
        dic[@"Content"]= dicArr;
        
        [self.recipeArray replaceObjectAtIndex:indexPath.section withObject:dic];

        _isOpen =NO;
        
    }else{
        
        [cell.collectionBnt setImage:[UIImage imageNamed:@"Icon_collectionYES"] forState:UIControlStateNormal];
        NSString * praiseCount = [NSString stringWithFormat:@"%d",a+1];
        
        cell.praiseLabel.text = praiseCount;
        
        dicArr[@"Praise"] = praiseCount;
        
        dic[@"Content"]= dicArr;
        
        [self.recipeArray replaceObjectAtIndex:indexPath.section withObject:dic];

        _isOpen =YES;

    }

}


@end
